package auto_deployment

import (
	"encoding/json"
	"net/http"
)

type GogsParam struct {
}
type OschinaParam struct {
	Password  string `json:"password,string"`
	Push_data struct {
		user_id int
		before  string
		after   string
		commits struct {
			message string
		}
	}
}

func IndexHandle(resp http.ResponseWriter, req *http.Request) {
	var show = `密码：真实密码_部署地方`
	resp.Write([]byte(show))
}
func GogsHandle(resp http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	var paramStr = req.PostFormValue("hook")

	resp.Write([]byte(paramStr))
}

func OschinaHandle(resp http.ResponseWriter, req *http.Request) {
	var err error
	req.ParseForm()
	var paramStr = req.PostFormValue("hook")
	paramStr = `{
    "password": "hod20x4hnkecy63", 
    "hook_name": "push_hooks", 
    "push_data": {
        "before": "26c9fb3533d1c63e3b822d58a8831b1caf6d9cd7", 
        "after": "219aa13d45276a5cae7436ecc66053da67ec2a2d", 
        "ref": "refs/heads/master", 
        "user_id": 449815, 
        "user_name": "gitlabu6d4bu8bd5u8d26u53f72", 
        "user": {
            "id": 449815, 
            "email": "gitlabtest_2@gitlab.com", 
            "name": "gitlabu6d4bu8bd5u8d26u53f72", 
            "time": "2015-11-06T14:51:55+08:00"
        }, 
        "repository": {
            "name": "test_gitosc_20151106145021322", 
            "url": "git@git.oschina.net:gitlab_test_1/test_gitosc_20151106145021322.git", 
            "description": "test_gitosc_20151106145021322 testting", 
            "homepage": "http://git.oschina.net/gitlab_test_1/test_gitosc_20151106145021322"
        }, 
        "commits": [
            {
                "id": "219aa13d45276a5cae7436ecc66053da67ec2a2d", 
                "message": "commit_access_test", 
                "timestamp": "2015-11-06T14:50:47+08:00", 
                "url": "http://git.oschina.net/gitlab_test_1/test_gitosc_20151106145021322/commit/219aa13d45276a5cae7436ecc66053da67ec2a2d", 
                "author": {
                    "name": "gitlab_test_2", 
                    "email": "gitlabtest_2@gitlab.com", 
                    "time": "2015-11-06T14:50:47+08:00"
                }
            }
        ], 
        "total_commits_count": 1, 
        "commits_more_than_ten": false
    }
}`
	var param = new(OschinaParam)
	err = json.Unmarshal([]byte(paramStr), param)
	if err != nil {
		resp.Write([]byte(err.Error()))
	}
	resp.Write([]byte(param.Password))
}

func Run() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", IndexHandle)
	mux.HandleFunc("/gogs", GogsHandle)
	mux.HandleFunc("/oschina", OschinaHandle)
	http.ListenAndServe(":8001", mux)
}
